import wx from 'weixin-js-sdk'
import Vue from 'vue'
import {weixinApi} from './../databus'


class Weixin {
  constructor () {
    this.wxReady = null
  }

  signature () {
    let options = {
      params: {
        url: 'http://weixin.donixtech.com/project/phone/'
      }
    }
    this.wxReady = new Promise((resolve, reject) => {
      (async() => {
        try {
          let result = await Vue.http.jsonp(weixinApi.signature, options)
          let data = result.data.data || {}
          let settings = Object.assign(DefaultConfig, data || {}, {
            appId: data.appid,
            nonceStr: data.noncestr,
            debug: false
          })
          wx.config(settings)
          wx.ready(() => {
            resolve(wx)
          })
          wx.error((err) => {
            reject(err)
          })
        } catch (err) {
          reject(err)
        }
      })()
    })
  }
}

export default new Weixin()
